package com.qf.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/category")
public class DtsAdminCategoryController {

@Autowired
DtsCategoryService categoryService;

    @RequestMapping("/list")
    public Object list(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                       @RequestParam(name = "limit", required = false, defaultValue = "10") Integer limit,
                       String sort, String order, Integer id,String name)
    {
        IPage<DtsCategory> ipage = categoryService.queryPage(page, limit, sort, order, name, id);

        Map<String,Object> data = new HashMap<>();

        data.put("total", ipage.getTotal());
        data.put("items", ipage.getRecords());

        return ResponseUtil.ok(data);
    }


    @RequestMapping("/l1")
    public Object list2(){

        List<Map<String,Object>> data=new ArrayList<>();
        List<DtsCategory> dtsCategories=categoryService.find();
        for (DtsCategory dtsCategory : dtsCategories) {
            Map<String,Object> map=new HashMap<>();
            map.put("value",dtsCategory.getId());
            map.put("label",dtsCategory.getName());
            data.add(map);
        }
        return ResponseUtil.ok(data);

    }
    @RequestMapping("/create")
    public Object create(@RequestBody DtsCategory category){
         category=categoryService.create(category);
        return ResponseUtil.ok(category);

    }

    @RequestMapping("/update")
    public Object update(@RequestBody DtsCategory category){
        category=categoryService.update(category);
        return ResponseUtil.ok(category);

    }
}
